
import numpy as np

class numpy_homework():
    def matrixEdgeFulfill(self):
        '''
        使用np.pad()即可
        '''
        myMatrix = np.array([[10, 34, 54, 23],
                            [31, 87, 53, 68],
                            [98, 49, 25, 11],
                            [84, 32, 67, 88]])
        myMatrix = np.pad(myMatrix,((1,1),(1,1)),'constant',constant_values = (0,0))
        print(myMatrix)
    
    def newMat(self):
        '''
        使用numpy.diag()
        '''
        myMat = np.diag(1+np.arange(4),k = -1)
        print(myMat)

    def chess(self):
        '''
        生成一个8*8的全0矩阵
        奇数行偶数下标填1
        偶数行奇数下标填1
        '''
        myMat = np.zeros((8,8), dtype = int)
        myMat[1::2, ::2] = 1
        myMat[::2, 1::2] = 1
        print(myMat)

    def solveLinerEquations(self):
        '''
        存入系数矩阵和解向量，直接使用numpy提供的linalg.solve()求解
        '''
        A = np.mat("3 4 2; 5 3 4; 8 2 7")
        b = np.array([10, 14, 20])
        x = np.linalg.solve(A, b)
        print ("A\n", A)
        print ("b\n", b)
        print ("Solution\n", x)
        print ("Check\n", np.dot(A , x))

    def reverse(self):
        '''
        反向读入一个数组即可
        '''
        myArray = np.array(np.arange(10))
        print("Init array\n",myArray)
        reverseArray = myArray[::-1]
        print("Reversed array\n", reverseArray)

    def maxAndMin(self):
        random = np.random.randint(-20, 20, (10, 10))
        print(random)
        print("Max\n", random.max())
        print("Min\n", random.min())
        


if __name__ == '__main__':
    homework = numpy_homework()
    #homework.matrixEdgeFulfill();
    #homework.newMat();
    #homework.chess();
    #homework.solveLinerEquations();
    #homework.reverse()
    homework.maxAndMin()


    